
package com.task.base;

import android.app.Activity;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;
import android.widget.Toast;

import java.text.DecimalFormat;
import java.text.NumberFormat;

public class ShortcutActivity extends Activity {

    private ImageView rotateImage;

    private View animView;

    private Rect rect;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.animlayout);
        rotateImage = (ImageView) findViewById(R.id.shortcut_anim);
        animView = findViewById(R.id.shortcut_layout);
        rect = getIntent().getSourceBounds();
    }

    @Override
    protected void onStart() {
        super.onStart();
        setPosition();
        handler.sendEmptyMessage(MSG_OPEN);

    }

    /**
     * 将单位dp数值转为px
     * 
     * @param dp
     * @return
     */
    private int dp2px(float dp) {
        float scale = this.getApplicationContext().getResources().getDisplayMetrics().density;
        return (int) (dp * scale + 0.5f);

    }

    /**
     * 设置动画图标位置
     */
    private void setPosition() {
        int centerX = rect.centerX();
        int centerY = rect.centerY();

        int left = centerX - dp2px(76 / 2);
        int top = centerY - dp2px(76 / 2) - dp2px(38);
        
        animView.setPadding(left, top, 0, 0);
    }

    private final static int MSG_START = 0;

    private final static int MSG_STOP = 1;

    protected static final int MSG_OPEN = 2;

    protected static final int MSG_CLOSE = 3;

    protected static final int MSG_CLEAR = 4;

    Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {

                case MSG_OPEN:
                    playOpenAnim();
                    handler.sendEmptyMessageDelayed(MSG_START, 500);
                    break;
                case MSG_START:
                    playAnim();
                    handler.sendEmptyMessageDelayed(MSG_CLOSE, 2000);
                    break;
                case MSG_CLOSE:
                    playCloseAnim();
                    handler.sendEmptyMessageDelayed(MSG_STOP, 300);
                    break;
                case MSG_STOP:
                    finish();
                    overridePendingTransition(0, 0);
                    handler.sendEmptyMessage(MSG_CLEAR);
                    break;
                case MSG_CLEAR:
                    Float free = Util.freeMemory(ShortcutActivity.this);
                    NumberFormat formater = DecimalFormat.getInstance();
                    Toast.makeText(ShortcutActivity.this,
                            formater.format(free) + getString(R.string.clear_memory_finish),
                            Toast.LENGTH_SHORT).show();
                    break;

            }

            super.handleMessage(msg);
        }

    };

    /**
     * 旋转动画
     */
    private void playAnim() {

        Animation animation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f);
        animation.setDuration(500);
        animation.setRepeatCount(2);

        rotateImage.startAnimation(animation);
    }

    /**
     * 关闭时缩小的动画
     */
    private void playCloseAnim() {
        Animation animIn = new ScaleAnimation(1f, 0.85f, 1f, 0.85f, Animation.ABSOLUTE,
                rect.centerX(), Animation.ABSOLUTE, rect.centerY() - dp2px(38));
        animIn.setDuration(300);
        animView.startAnimation(animIn);
    }

    /**
     * 打开时放大的动画
     */
    private void playOpenAnim() {
        Animation animIn = new ScaleAnimation(0.85f, 1f, 0.85f, 1f, Animation.ABSOLUTE,
                rect.centerX(), Animation.ABSOLUTE, rect.centerY() - dp2px(38));
        animIn.setDuration(500);
        animView.startAnimation(animIn);

    }

}
